<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/20
 * Time: 11:31
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Question extends Base
{
    /**
     * 轮播图首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $where['q_status'] = ['neq', 3];
            if(isset($keyword)&&$keyword!="")
            {
                $where['q_title'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($status)&&$status!="")
            {
                $where['q_status'] = $status;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('question')->where($where)->count();// 获取总条数
            $lists = Db::name('question')
                ->where($where)
                ->field('q_content',true)
                ->page($Nowpage,$limits)
                ->order('q_status asc,q_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加banner
     * @return mixed|\think\response\Json
     */
    public function bannerAdd(){
        if(Request::instance()->isPost()){
            $banner_title = trim(input('banner_title'), ' ');
            $banner_status = input('banner_status');
//            $banner_sort = input('banner_sort');
            $banner_content = trim(input('banner_content'), ' ');
            $data = [
                'q_title' => $banner_title,
                'q_status' => $banner_status,
                'q_content' => $banner_content,
                'q_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_question')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('question/banneradd');
    }
    
    /**
     * 编辑轮播图
     * @return mixed|\think\response\Json
     */
    public function bannerEdit(){
        if(Request::instance()->isPost()){
            $id = input('banner_id');
            $banner_title = trim(input('banner_title'), ' ');
            $q_status = trim(input('q_status'), ' ');
            $banner_content = trim(input('banner_content'), ' ');
            $data = [
                'q_title' => $banner_title,
                'q_status' => $q_status,
                'q_content' => $banner_content,
                'q_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_question')->where('q_id', $id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $banner_id = input('banner_id');
        // banner
        $banner = Db::table('up_question')
            ->where('q_id', $banner_id)
            ->find();
        $this->assign([
            'q_id' => $banner_id,
            'banner' => $banner
        ]);
        return $this->fetch('question/banneredit');
    }
    
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            'banner_sort'=>input('value'),
        ];
        $res = Db::name('banner')->where('banner_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function bannerOn(){
        $banner_id = input('id');
        $res = Db::table('up_question')->where('q_id', $banner_id)->setField('q_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '上架成功']);
        }else{
            return json(['code' => 100, 'msg' => '上架失败']);
        }
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function bannerDown(){
        $banner_id = input('id');
        $res = Db::table('up_question')->where('q_id', $banner_id)->setField('q_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '下架成功']);
        }else{
            return json(['code' => 100, 'msg' => '下架失败']);
        }
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function bannerDel(){
        $banner_id = input('id');
        $res = Db::table('up_question')->where('q_id', $banner_id)->setField('q_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
}